﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLNS_v1.Bus;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.BandedGrid;

namespace QLNS_v1.Form
{
    public partial class uclLuongNhanVien : DevExpress.XtraEditors.XtraUserControl
    {
        NhanVien_Bus nvb = new NhanVien_Bus();
        PhuCap_Bus pcb = new PhuCap_Bus();
        ChamCong_Bus ccb = new ChamCong_Bus();
        public uclLuongNhanVien()
        {
            InitializeComponent();
            gridBand1.Fixed = FixedStyle.Left;
            colMaNV.Fixed = FixedStyle.Left;
            colHT.Fixed = FixedStyle.Left;
            gridBand7.Fixed = FixedStyle.Right;
            advBandedGridView1.BestFitColumns();
            cboThang.EditValue = DateTime.Now.Month;
            cboNam.EditValue = DateTime.Now.Year;
            cboNam.EditValueChanged += cboNam_EditValueChanged;
            cboThang.EditValueChanged += cboThang_EditValueChanged;
            //List<PhuCap> listphucap = pcb.ListPhuCap();
            //int index = 0;
            //foreach (var a in listphucap)
            //{
            //    BandedGridColumn column = new BandedGridColumn();
            //    column.VisibleIndex = index;
            //    column.Caption = a.TenPhuCap;
            //    column.Name = "PhuCapChucVu";
            //    column.FieldName = a.MaPhuCap;
            //    column.Width = 50;
            //    gridBand4.Columns.Add(column);
            //    index++;
            //}
            //gridControl1.DataSource = listphucap;

            #region COde động mẫu
            // 
            //GridBand c = new GridBand();
            //c.AppearanceHeader.Options.UseTextOptions = true;
            //c.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            //c.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            //c.AppearanceHeader.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            //c.Caption = "Thông tin nhân viên";
            //c.ImageAlignment = System.Drawing.StringAlignment.Center;
            //c.Name = "c";
            //c.VisibleIndex = 7;
            //c.Width = 156;
            //advBandedGridView1.Bands.Add(c);

            //GridBand c = new GridBand();
            //c.AppearanceHeader.Options.UseTextOptions = true;
            //c.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            //c.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            //c.AppearanceHeader.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            //c.Caption = "Thông tin nhân viên";
            //c.ImageAlignment = System.Drawing.StringAlignment.Center;
            //c.Name = "c";
            //c.VisibleIndex = 2;
            //c.Width = 156;
            //BandedGridColumn a = new BandedGridColumn();
            //a.VisibleIndex = 0;
            //a.Caption = "abc";
            //a.Width = 50;
            //gridBand2.Columns.Add(a);
            //BandedGridColumn b = new BandedGridColumn();
            //b.VisibleIndex = 1;
            //b.Caption = "xyz";
            //b.Width = 50;
            //gridBand2.Columns.Add(b);

            //BandedGridColumn g = new BandedGridColumn();
            //g.VisibleIndex = 0;
            //g.Caption = "xyz";
            //g.Width = 50;
            //gridBand1.Columns.Add(g);

            //BandedGridColumn h = new BandedGridColumn();
            //h.VisibleIndex = 1;
            //h.Caption = "xyz";
            //h.Width = 50;
            //gridBand1.Columns.Add(h);
            //advBandedGridView1.Bands.Add(c); 
            #endregion
            
        }

        void cboThang_EditValueChanged(object sender, EventArgs e)
        {
            if (CreateChamCong(int.Parse(cboThang.EditValue.ToString()),int.Parse(cboNam.EditValue.ToString())) == false)
                return;
            try { 
            gridControl1.DataSource = nvb.ListNhanVien_Luong(int.Parse(cboThang.EditValue.ToString()), int.Parse(cboNam.EditValue.ToString()));
        }
            catch { }
        }

        void cboNam_EditValueChanged(object sender, EventArgs e)
        {
            if (CreateChamCong(int.Parse(cboThang.EditValue.ToString()), int.Parse(cboNam.EditValue.ToString())) == false)
                return;
            try
            {
                gridControl1.DataSource = nvb.ListNhanVien_Luong(int.Parse(cboThang.EditValue.ToString()), int.Parse(cboNam.EditValue.ToString()));
            }
            catch { }
        }

        private void uclLuongNhanVien_Load(object sender, EventArgs e)
        {
            
            GetThangNam();
            gridControl1.DataSource = nvb.ListNhanVien_Luong(DateTime.Now.Month,DateTime.Now.Year);
        }
        private void GetThangNam()
        {
            txtThangNam.EditValue = cboThang.EditValue.ToString() + " - " + cboNam.EditValue.ToString();
        }
        private void cboSubThang_EditValueChanged(object sender, EventArgs e)
        {
            GetThangNam();
        }

        private void repositoryItemCalcEdit1_EditValueChanged(object sender, EventArgs e)
        {
            GetThangNam();
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private bool CreateChamCong(int thang,int nam)
        {
            int maxthang = ccb.GetMaxThang(thang, nam);
            if (maxthang != -1)
            {
                DialogResult re = XtraMessageBox.Show("Tháng này chưa tạo bảng chấm công, Bạn có muốn tạo bảng chấm công không", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (re == DialogResult.Yes)
                {
                    NhanVien_Bus nvb = new NhanVien_Bus();

                    List<NhanVien_BTO> listnhanvien = nvb.ListNhanVien();
                    foreach (var a in listnhanvien)
                    {
                        ccb.CreateChamCong(thang,nam, a.NhanVienID);
                    }
                    XtraMessageBox.Show("Đã tạo bảng chấm công thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return true;

                }

                else
                    return false;
            }
            return true;
        }

    }
}
